package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.ObservableArrayList;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;

/**
 * Created by zxh on 16/11/25.
 */

public class MyScoreModel extends MyAccountModel {

    public MyScoreModel(Context context) {
        super(context);
        super.getHandler().setTitle("我的积分");
    }

    @Override
    public void onClickRecharge(View view) {

    }

    @Override
    public void onClickWithdraw(View view) {

    }

    /**
     * 点击积分优惠活动
     *
     * @param view
     */
    public void onClickActive(View view) {
        String url = WebApi.getActive(getContext());
        HomeActivity.startWebViewFragment(getContext(), url);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                LinkedTreeMap map = WebApi.getActive(getContext());
//                if (map != null) {
//                    final String url = (String) map.get("url");
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            HomeActivity.startWebViewFragment(getContext(), url);
//                        }
//                    });
//                }
//            }
//        }).start();
    }

    @Override
    public void load(final int page) {
        final Activity activity = (Activity) getContext();
        final XListAdapterInterface adapter = getAdapter();
        LoadingDialog.getInstance(activity).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                setBalance(WebApi.getScore(activity));
                ArrayList list = WebApi.getScoreDetail(activity, page);
                try {
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        BalanceItem balanceItem = new BalanceItem(activity);
                        balanceItem.setDate(Utils.getDateFromTimestamp(map.get("systime")));
                        balanceItem.setType((String) map.get("remark"));
                        balanceItem.setValue(Utils.getDoubleValue(map.get("amount")));
                        adapter.add(balanceItem);
                    }
                    if (list.size() > 0) {
                        setCurrentPage(page);
                    }
                    notifyDataSetChanged(list);
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                LoadingDialog.getInstance(activity).dismiss();
            }
        }).start();
    }
}
